<template>
  <!-- 服务类型 -->
  <div>
    <span v-if="readonly">{{ selectData }}</span>
    <el-select
      v-else
      :disabled="readonly"
      @change="changeData"
      v-model="checkedData"
      :placeholder="$t('请选择服务类型')"
    >
      <el-option
        v-for="item in options"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      >
      </el-option>
    </el-select>
  </div>
</template>
<script>
import nonStandardService from '@/services/nonStandardService'
import eventBus from '@/assets/utils/eventBus.js'
export default {
  name: 'serviceType',
  props: {
    detail: {
      type: Object,
      default: () => {
        return {}
      },
    },
    value: {},
    disabled: Boolean,
    parameter: {
      type: Object,
      default: () => {
        return {}
      },
    },
  },
  data() {
    return {
      options: [],
      readonly: false,
      selectData: null,
      checkedData: null,
    }
  },
  created() {
    this.readonly = Boolean(this.$route.query.readonly)
    if (!this.readonly) {
      this.queryData()
    }
    if (this.value) {
      this.setData()
    }
  },
  methods: {
    queryData() {
      nonStandardService
        .queryServiceTypeInfo({ ecologyCode: 'WMS', eventState: 1 })
        .then((res) => {
          if (res.code == 200) {
            this.options =
              res.data.amsPriceEventInfos &&
              res.data.amsPriceEventInfos.map((it) => {
                return {
                  ...it,
                  label: it.eventCode + it.eventName,
                  value: it.eventCode,
                }
              })
          }
        })
    },
    changeData(val) {
      eventBus.$emit('serviceTypeChange', val)
      let info = this.options.find((it) => it.value == val) || {}
      this.selectData = info.label
      this.$emit('input', String(this.checkedData))
    },
    setData() {
      this.selectData = JSON.parse(this.value).value
      this.checkedData = JSON.parse(this.value).checkedData
      this.options = [
        {
          value: this.checkedData,
          label: this.selectData,
        },
      ]
    },
    getData() {
      return {
        value: this.selectData,
        checkedData: this.checkedData,
      }
    },
  },
}
</script>
